import { createRouter,createWebHistory } from "vue-router";
import { login } from "./utils/api";
import {useLoginStore} from "./store/useLoginStore"

let router =  createRouter({
    history:createWebHistory(),
    routes:[
        {
            path:"/",
            name:'index',
            component:()=>import("./views/index/index.vue")
        },
        {
            path:"/index",
            redirect:"/"
        },
        {
            path:"/ticketList",
            name:"ticketList",
            component:()=>import("./views/ticketList/ticketList.vue")
        },
        {
            path:"/ticketDesc",
            name:"ticketDesc",
            component:()=>import("./views/ticketDesc/ticketDesc.vue")
        },
        {
            path:"/ticketChooseSeat",
            name:"ticketChooseSeat",
            component:()=>import("./views/ticketChooseSeat/ticketChooseSeat.vue"),
            meta:{
                requireAuth:true
            }
        },
        {
            path:"/orderConfirm",
            name:"orderConfirm",
            component:()=>import("./views/orderConfirm/orderConfirm.vue"),
            meta:{
                requireAuth:true
            }
        },
        {
            path:"/paymethod",
            name:"paymethod",
            component:()=>import("./views/paymethod/paymethod.vue"),
            meta:{
                requireAuth:true
            }
        },
        {
            path:"/wechatpay",
            name:"wechatpay",
            component:()=>import("./views/wechatpay/wechatpay.vue"),
            meta:{
                requireAuth:true
            }
        },
        {
            path:"/enroll",
            name:'enroll',
            component:()=>import("./views/enroll/index.vue")
        },
        {
            path:"/login",
            name:'login',
            component:()=>import("./views/login/index.vue")
        },
        {
            path:"/multiplePage",   
            component:()=>import("./pages/multiplePage/index.vue")
        },
        {
            path:"/home",
            component:()=>import("./views/home/home.vue"),
            meta:{
                requireAuth:true
            },
            children:[
                {
                    path:"/orderList",
                    component:()=>import("./views/home/orderList/orderList.vue")
                },
                {
                    path:"/ticketBuyer",
                    component:()=>import("./views/home/ticketBuyer/ticketBuyer.vue")
                }
            ]
        }
    ]
})

router.beforeEach((to,from,next)=>{
    window.scrollTo(0,0);
    let loginStore = useLoginStore();
    let loginUser = loginStore.loginUser;
    if(to.meta.requireAuth){
        // let loginUser = window.localStorage.getItem("login");
        if(loginUser){
            let ttl=loginUser.tokenVo.expTime*60*1000;//token失效的临界时间
            let ttl2=loginUser.tokenVo.genTime+loginUser.tokenVo.expTime*60*1000*2;//有效时间
            let now = new Date().getTime();
            if(now>ttl2){
                next({path:'/login',query:{toFrom:to.fullPath}});
            }else if(now>ttl){
                //续命
                loginStore.refreshToken();
                next();
            }else{
                next();
            }
        }else{
            next({path:'/login',query:{toFrom:to.fullPath}});
        }
    }else{
        next();
    }
})

export default router;